package com.delicious.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.delicious.dao.impl.CategoryDaoImpl;
import com.delicious.dao.impl.FoodsDaoImpl;
import com.delicious.entity.Category;
import com.delicious.entity.Foods;

public class FoodsListServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Category> clist = new CategoryDaoImpl().getList();
		int cid = 0;
		if(request.getParameter("cid") != null && !request.getParameter("cid").isEmpty())
			cid = Integer.parseInt(request.getParameter("cid"));
		String name = "";
		if(request.getParameter("name")!=null && !request.getParameter("name").isEmpty()){
			name = request.getParameter("name");
		}
		int id = 0;
		if(request.getParameter("id") != null && !request.getParameter("id").isEmpty()){
			id = Integer.parseInt(request.getParameter("id"));
		}
		new FoodsDaoImpl().deleteDataById(id);
		List<Foods> flist = new FoodsDaoImpl().getAllFoods(cid,name);
		request.setAttribute("category", clist);
		request.setAttribute("foods", flist);
		request.setAttribute("categoryid", cid);
		request.setAttribute("cid", cid);
		request.setAttribute("name", name);
		
		request.getRequestDispatcher("/admin/foodsList.jsp").forward(request, response);
	}
	
	public static void main(String[] args) {
		List<Foods> flist = new FoodsDaoImpl().getAllFoods(1,"鸡");
		for(Foods f : flist){
			System.out.println(f.getChineseName()+"\t"+f.getEnglishName());
		}
	}

}
